package it.appcatalogobase;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;

public class ListProductsActivity extends ActionBarActivity {

	private ProdottoDataSource datasourcepr;
	private GridView gridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_products);
		LoadTask loadT = new LoadTask(this, getIntent().getStringExtra("type"), (GridView) findViewById(R.id.gridview));
		loadT.execute();
		// // if (savedInstanceState == null) {
		// // getSupportFragmentManager().beginTransaction()
		// // .add(R.id.container, new PlaceholderFragment()).commit();
		// // }
		// datasourcepr = new ProdottoDataSource(this);
		// datasourcepr.open();
		// if (getIntent().getStringExtra("type").equals("all")) {
		//
		// // datasourcepr.createProdotto("Caffè amaro", "amaro amarissimo",
		// // "Caffè", "caffe_amaro");
		// // datasourcepr.createProdotto("Caffè dolce", "dolce dolcissimo",
		// // "Caffè", "caffe_dolce");
		// // datasourcepr.createProdotto("Birra", "boooooooo", "altro",
		// // "birra");
		//
		// final List<Prodotto> valuesPr = datasourcepr.getAllProdotti();
		// Iterator<Prodotto> it = valuesPr.iterator();
		// final String[] names = new String[valuesPr.size()];
		// final String[] ids = new String[valuesPr.size()];
		// final String[] images = new String[valuesPr.size()];
		// final String[] descr = new String[valuesPr.size()];
		// final String[] colori = new String[valuesPr.size()];
		// int counter = 0;
		// while (it.hasNext()) {
		// Prodotto p = it.next();
		// names[counter] = p.getNome();
		// ids[counter] = String.valueOf(p.getId());
		// images[counter] = p.getImmagine();
		// descr[counter] = p.getDescrizione();
		// colori[counter] = p.getColori();
		// counter++;
		// }
		// if (counter == 0) {
		// Toast t = Toast.makeText(this,
		// "NESSUN PRODOTTO PRESENTE NEL CATALOGO", Toast.LENGTH_LONG);
		// t.setGravity(Gravity.CENTER, 0, 0);
		// t.show();
		// }
		// gridView = (GridView) findViewById(R.id.gridview);
		// gridView.setAdapter(new ImageAdapter(this, names, ids, images));
		// gridView.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View v, int position,
		// long id) {
		// Intent i = new Intent(ListProductsActivity.this,
		// ProductDettaglioActivity.class);
		// i.putExtra("id", ids[position]);
		// i.putExtra("name", names[position]);
		// i.putExtra("des", descr[position]);
		// i.putExtra("imm", images[position]);
		// if (valuesPr.size() > 1)
		// i.putExtra("type", "all");
		// else
		// i.putExtra("type", "single");
		// i.putExtra("colori", colori[position]);
		// startActivity(i);
		// }
		// });
		// } else if (getIntent().getStringExtra("type").equals("cat")) {
		// String cat = getIntent().getStringExtra("cat");
		// Long id = getIntent().getLongExtra("id", 1);
		// final List<Prodotto> valuesPr =
		// datasourcepr.getAllProdottiFromCat(cat);
		// Iterator<Prodotto> it = valuesPr.iterator();
		// final String[] names = new String[valuesPr.size()];
		// final String[] ids = new String[valuesPr.size()];
		// final String[] images = new String[valuesPr.size()];
		// final String[] descr = new String[valuesPr.size()];
		// final String[] colori = new String[valuesPr.size()];
		// int counter = 0;
		// while (it.hasNext()) {
		// Prodotto p = it.next();
		// names[counter] = p.getNome();
		// ids[counter] = String.valueOf(p.getId());
		// images[counter] = p.getImmagine();
		// descr[counter] = p.getDescrizione();
		// colori[counter] = p.getColori();
		// counter++;
		// }
		// if (counter == 0) {
		// Toast t = Toast.makeText(this,
		// "NESSUN PRODOTTO PRESENTE PER LA CATEGORIA SELEZIONATA",
		// Toast.LENGTH_LONG);
		// t.setGravity(Gravity.CENTER, 0, 0);
		// t.show();
		// }
		// gridView = (GridView) findViewById(R.id.gridview);
		// gridView.setAdapter(new ImageAdapter(this, names, ids, images));
		// gridView.setOnItemClickListener(new OnItemClickListener() {
		// public void onItemClick(AdapterView<?> parent, View v, int position,
		// long id) {
		// Intent i = new Intent(ListProductsActivity.this,
		// ProductDettaglioActivity.class);
		// i.putExtra("id", ids[position]);
		// i.putExtra("name", names[position]);
		// i.putExtra("des", descr[position]);
		// i.putExtra("imm", images[position]);
		// if (valuesPr.size() > 1)
		// i.putExtra("type", "cat");
		// else
		// i.putExtra("type", "single");
		// i.putExtra("colori", colori[position]);
		// startActivity(i);
		// // i.putExtra("id", ((TextView)
		// // v.findViewById(R.id.grid_item_id)).getText().toString());
		// }
		// });
		// }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_products, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		// if (id == R.id.action_settings) {
		// return true;
		// }
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent i = new Intent(this, Main2Activity.class);
		i.putExtra("frag", "catalogo");
		super.onBackPressed();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		//
		//        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
		//            setContentView(R.layout.landscapeView);
		//
		//        } else {
		//            setContentView(R.layout.portraitView);
		//        }
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	// public static class PlaceholderFragment extends Fragment {
	//
	// public PlaceholderFragment() {
	// }
	//
	// @Override
	// public View onCreateView(LayoutInflater inflater, ViewGroup container,
	// Bundle savedInstanceState) {
	// View rootView = inflater.inflate(R.layout.fragment_list_products,
	// container, false);
	// return rootView;
	// }
	// }

}
